<?php

/**
 * This is the model class for registrationForm.
 *
 * 
 * @property integer $id
 * @property string $uid
 * @property string $firstname
 * @property string $lastname
 * @property string $company_information
 * @property string $company_name
 * @property string $address
 * @property string $city
 * @property string $state
 * @property string $zipcode
 * @property string $phone
 * @property string $fax
 * @property string $password
 * @property string $password_repeat
 * @property string $email
 */
class RegistrationForm extends CFormModel {

    public  $firstname,
            $lastname,
            $company_information,
            $company_name,
            $address,
            $city,
            $state,
            $zipcode,
            $phone,
            $fax,
            $password,
            $password_repeat,
            $email;

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('email,password,password_repeat,firstname, lastname, company_information, company_name, address, city, state, zipcode, phone, fax', 'required'),
            array('firstname, lastname', 'length', 'max' => 25),
            array('company_name, address', 'length', 'max' => 50),
            array('city', 'length', 'max' => 20),
            array('state', 'length', 'max' => 2),
            array('zipcode', 'length', 'max' => 10),
            array('phone, fax', 'length', 'max' => 12),
            array('password', 'compare', 'compareAttribute' => 'password_repeat'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
        );
    }

    public function register(){
        $umodel = new Users;
        $umodel->setScenario('register');
        $umodel->attributes = $this->getAttributes();
        $umodel->real_name = $this->firstname.' '.  $this->lastname;
        $umodel->utype = 0;
        $umodel->validate();

        if($umodel->save()){
          
            $details = new UserDetails;
            $details->uid = $umodel->id;
            $details->attributes = $this->getAttributes();

            return $details->save();
        }
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'firstname' => 'Firstname',
            'lastname' => 'Lastname',
            'company_information' => 'Company Information',
            'company_name' => 'Company Name',
            'address' => 'Address',
            'city' => 'City',
            'state' => 'State',
            'zipcode' => 'Zipcode',
            'phone' => 'Phone',
            'fax' => 'Fax',
        );
    }

}
